package com.yuwei.home.adapter

import android.view.LayoutInflater

import android.view.ViewGroup
import android.widget.ImageView
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.yuwei.common.entity.SimpleVideoEntityItem
import com.yuwei.home.R
import com.yuwei.image.glide.GlideImageLoader

class PagingAdapter : PagingDataAdapter<SimpleVideoEntityItem, BaseViewHolder>(VIDEO_COMPARATOR) {
    override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
        val item = getItem(position)
        if(item!=null){
            val image = holder.getView<ImageView>(R.id.simple_video_image)
            GlideImageLoader.loadImage(image.context,image,item.videomainimag)
            holder.setText(R.id.simple_video_desc,item.description)
            val icon = holder.getView<ImageView>(R.id.simple_video_icon)
            GlideImageLoader.loadCircleImage(icon.context,icon,item.avatar_url)
            holder.setText(R.id.simple_video_author_name,item.name)
        }

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_simple_video, parent, false)
        return BaseViewHolder(view)
    }
    companion object {
        val VIDEO_COMPARATOR = object : DiffUtil.ItemCallback<SimpleVideoEntityItem>() {
            override fun areItemsTheSame(oldItem: SimpleVideoEntityItem, newItem: SimpleVideoEntityItem) =
                oldItem.id == newItem.id

            override fun areContentsTheSame(oldItem: SimpleVideoEntityItem, newItem: SimpleVideoEntityItem) =
                oldItem == newItem
        }
    }

}
